<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button type="success" @click="isdraw('新增')">新增</el-button>
        <el-button type="warning" @click="isdraw('修改')">修改</el-button>
        <el-button type="danger" @click="delonData">删除</el-button>
      </el-col>
      <el-col :span="12">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          class="input-with-select"
        >
          <el-select
            v-model="select"
            slot="prepend"
            style="width: 130px"
            placeholder="请选择"
          >
            <el-option label="餐厅名" value="1"></el-option>
            <el-option label="订单号" value="2"></el-option>
            <el-option label="用户电话" value="3"></el-option>
          </el-select>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchData"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>
    <div style="margin-top: 30px">
      <!--  -->
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="700"
        @selection-change="handleSelectionChange"
      >
        <el-table-column fixed type="selection" width="55"></el-table-column>
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column prop="name" label="姓名" width="120"></el-table-column>
        <el-table-column prop="age" label="年龄" width="120"></el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="loginName"
          label="用户名"
          width="300"
        ></el-table-column>
        <el-table-column
          prop="loginKey"
          label="密码"
          width="120"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteRow(scope.row, '删除')"
              type="text"
              size="small"
              >移除</el-button
            >
            <el-button
              @click.native.prevent="deleteRow(scope.row, '修改')"
              type="text"
              size="small"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--  -->
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
    ></el-pagination>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
    >
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-radio-group v-model="form.gender">
              <el-radio label="男" value="男"></el-radio>
              <el-radio label="女" value="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input v-model="form.loginName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="form.loginKey" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="图片" :label-width="formLabelWidth">
            <el-input v-model="form.avatar" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm('取消')">取 消</el-button>
          <el-button type="primary" @click="cancelForm('确定')"
            >确 定</el-button
          >
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  layout: "mine",
  data() {
    return {
      input: "",
      select: "",
      drawer: false,
      title: "新增",
      currentPage: 1,
      form: {},
      formLabelWidth: "80px",
      pageSize: 10,
      multipleSelection: [],
    };
  },
  async fetch({ $axios, store }) {
    // const { data } = await $axios.get(
    //   "http://localhost:3001/users/user-list?pageSize=10"
    // );
    // store.state.list = data.data;
    store.dispatch("users/getData");
  },
  methods: {
    ...mapActions("users", ["getData", "addData", "editData", "delData"]),
    searchData() {
      console.log("搜索");
    },
    delonData() {
      if (!this.multipleSelection) return;
      let ids = this.multipleSelection.map((item) => {
        return item.id;
      });
      this.delUser(ids);
    },
    async delUser(id) {
      await this.delData(id.join(","));
      this.getData();
    },
    isdraw(v, row) {
      // 如果是复选框的修改
      if (!row) {
        // 点击的是修改 但是没有点击复选框
        if (v == "修改" && this.multipleSelection.length != 1) {
          return;
        }
      }
      if (v == "修改") {
        if (!row) {
          let val = this.multipleSelection[0];
          this.form = { ...val };
        } else {
          // 表格内修改  有row
          this.form = { ...row };
        }
      }
      this.title = v;
      this.drawer = !this.drawer;
    },
    handleClose(done) {
      done();
    },
    async cancelForm(v) {
      if (v == "确定") {
        if (this.title == "修改") {
          await this.editData(this.form);
        } else {
          await this.addData(this.form);
        }
        this.getData();
        this.form = {};
      }
      this.drawer = false;
    },
    handleSelectionChange(val) {
      // console.log(val);
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    deleteRow(row, v) {
      if (v == "删除") {
        this.delUser([row.id]);
      } else {
        this.isdraw(v, row);
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      // console.log(`当前页: ${val}`);
    },
  },
  computed: {
    ...mapState("users", ["data"]),
    tableData() {
      return this.data;
    },
  },
};
</script>

<style scoped>
</style>